package com.d3works.apps.talent.db.repo;

import com.d3works.apps.talent.db.TrainingSession;
import com.d3works.apps.talent.exception.TalentExceptionMessage;
import com.d3works.apps.talent.service.TrainingSessionService;
import com.d3works.i18n.I18n;
import com.google.inject.Inject;
import javax.validation.ValidationException;

public class TrainingSessionTalentRepository extends TrainingSessionRepository {

  @Inject private TrainingSessionService trainingSessionService;

  @Override
  public TrainingSession save(TrainingSession trainingSession) {

    if (trainingSession.getFromDate().isAfter(trainingSession.getToDate())) {
      throw new ValidationException(I18n.get(TalentExceptionMessage.INVALID_DATE_RANGE));
    }

    trainingSession.setFullName(trainingSessionService.computeFullName(trainingSession));

    return super.save(trainingSession);
  }
}
